// Copyright (c) 2025, hzmes and contributors
// For license information, please see license.txt

frappe.ui.form.on("HSF Certification Task", {
        onload_post_render(frm){
                frappe.dom.set_style(`#form-tabs > li {
                        background-color: #86e68e;
                        box-shadow: 1px 1px 8px lightgray;
                        color: green;
                        border-style: solid;
                }`)
        },
        onload: function(frm) {
		// 隐藏子表单[添加行]按钮
		frm.get_field('msds').grid.cannot_add_rows = true;
                frm.get_field('rohs').grid.cannot_add_rows = true;
                frm.get_field('hf').grid.cannot_add_rows = true;
                frm.get_field('reach').grid.cannot_add_rows = true;
                frm.get_field('others').grid.cannot_add_rows = true;
                frm.get_field('self_test').grid.cannot_add_rows = true;
	},
	refresh(frm) {
                // 隐藏左侧栏
                $('.col-lg-2').hide();
                $(".add-attachment-btn").hide();
                $(".add-assignment-btn").hide();
                $(".add-review-btn").hide();
                $(".share-doc-btn").hide();
                $(".form-assignments").hide();
                $(".reviews-label").hide();
                $(".tags-label").hide();
                $(".form-shared").hide();
                $(".form-tags").hide();
                $(".remove-btn").hide();
                $(".form-sidebar-stats").hide();
                $(".reviews").hide();

                // 增加msds子表按钮
                frm.fields_dict["msds"].grid.add_custom_button(__('Copy From FMD'), 
                function() {
                        frappe.call({
                                method: "copy_from_fmd",
                                doc: frm.doc,
                                args: {subtable: "msds"},
                                freeze: true,
                                freeze_message: __("running..."),
                                callback: function(r) { refresh_field("msds");}
                        });
                });
                frm.fields_dict["msds"].grid.grid_buttons.find('.btn-custom').removeClass('btn-default').addClass('btn-primary');
                // 增加rohs子表按钮
                frm.fields_dict["rohs"].grid.add_custom_button(__('Copy From FMD'), 
                function() {
                        frappe.call({
                                method: "copy_from_fmd",
                                doc: frm.doc,
                                args: {subtable: "rohs"},
                                freeze: true,
                                freeze_message: __("running..."),
                                callback: function(r) { refresh_field("rohs");}
                        });
                });
                frm.fields_dict["rohs"].grid.grid_buttons.find('.btn-custom').removeClass('btn-default').addClass('btn-primary');
                 // 增加msds子表按钮
                 frm.fields_dict["hf"].grid.add_custom_button(__('Copy From FMD'), 
                 function() {
                         frappe.call({
                                 method: "copy_from_fmd",
                                 doc: frm.doc,
                                 args: {subtable: "hf"},
                                 freeze: true,
                                 freeze_message: __("running..."),
                                 callback: function(r) { refresh_field("hf");}
                         });
                 });
                 frm.fields_dict["hf"].grid.grid_buttons.find('.btn-custom').removeClass('btn-default').addClass('btn-primary');
                // 增加reach子表按钮
                frm.fields_dict["reach"].grid.add_custom_button(__('Copy From FMD'), 
                function() {
                        frappe.call({
                                method: "copy_from_fmd",
                                doc: frm.doc,
                                args: {subtable: "reach"},
                                freeze: true,
                                freeze_message: __("running..."),
                                callback: function(r) { refresh_field("reach");}
                        });
                });
                frm.fields_dict["reach"].grid.grid_buttons.find('.btn-custom').removeClass('btn-default').addClass('btn-primary');      
                // 增加others子表按钮
                frm.fields_dict["others"].grid.add_custom_button(__('Copy From FMD'), 
                function() {
                        frappe.call({
                                method: "copy_from_fmd",
                                doc: frm.doc,
                                args: {subtable: "others"},
                                freeze: true,
                                freeze_message: __("running..."),
                                callback: function(r) { refresh_field("others");}
                        });
                });
                frm.fields_dict["others"].grid.grid_buttons.find('.btn-custom').removeClass('btn-default').addClass('btn-primary');      
                // 增加self_test子表按钮
                frm.fields_dict["self_test"].grid.add_custom_button(__('Copy From FMD'), 
                function() {
                        frappe.call({
                                method: "copy_from_fmd",
                                doc: frm.doc,
                                args: {subtable: "self_test"},
                                freeze: true,
                                freeze_message: __("running..."),
                                callback: function(r) { refresh_field("self_test");}
                        });
                });
                frm.fields_dict["self_test"].grid.grid_buttons.find('.btn-custom').removeClass('btn-default').addClass('btn-primary');      
                // 设置子表read_only
                frm.toggle_enable('hsf_fmd', frm.doc.fmd_status != 'Reviewed' && frm.doc.fmd_status != 'Unreviewed');
                frm.toggle_enable('msds', frm.doc.msds_status != 'Reviewed' && frm.doc.msds_status != 'Unreviewed'); 
                frm.toggle_enable('rohs', frm.doc.rohs_status != 'Reviewed' && frm.doc.rohs_status != 'Unreviewed'); 
                frm.toggle_enable('hf', frm.doc.hf_status != 'Reviewed' && frm.doc.hf_status != 'Unreviewed'); 
                frm.toggle_enable('reach', frm.doc.reach_status != 'Reviewed' && frm.doc.reach_status != 'Unreviewed'); 
                frm.toggle_enable('others', frm.doc.others_status != 'Reviewed' && frm.doc.others_status != 'Unreviewed'); 
                frm.toggle_enable('special', frm.doc.special_status != 'Reviewed' && frm.doc.special_status != 'Unreviewed'); 
                frm.toggle_enable('self_test', frm.doc.self_test_status != 'Reviewed' && frm.doc.self_test_status != 'Unreviewed');     
        },
        // FMD
        fmd_supplier_check: function(frm) {
                if (frm.doc.fmd_status == 'Pending' || frm.doc.fmd_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                                () => { frm.set_value('fmd_status', 'Unreviewed');frm.refresh_field('fmd_status'); frm.save() },'Continue',true
                                )
                }
                if (frm.doc.fmd_status == 'Reviewed') {frappe.msgprint('FMD状态已审核!')}
        },
        fmd_review: function(frm) {
                if (frm.doc.fmd_status == 'Unreviewed') {frm.set_value('fmd_status', 'Reviewed');frm.refresh_field('fmd_status'); frm.save() }
                if (frm.doc.fmd_status == 'Pending' || frm.doc.fmd_status == 'Reject') {frappe.msgprint('FMD状态未确认!')}
        },
        fmd_unreview: function(frm) {
                if (frm.doc.fmd_status == 'Unreviewed' || frm.doc.fmd_status == 'Reviewed') {frm.set_value('fmd_status', 'Reject');frm.refresh_field('fmd_status'); frm.save() }
                if (frm.doc.fmd_status == 'Pending') {frappe.msgprint('FMD状态未确认!')}
        },
        // MSDS
        msds_supplier_check: function(frm) {
                if (frm.doc.msds_status == 'Pending' || frm.doc.msds_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                                () => { frm.set_value('msds_status', 'Unreviewed');frm.refresh_field('msds_status'); frm.save()  },'Continue',true
                                )
                }
                if (frm.doc.msds_status == 'Reviewed') {frappe.msgprint('MSDS状态已审核!')}
        },
        msds_review: function(frm) {
                if (frm.doc.msds_status == 'Unreviewed') {frm.set_value('msds_status', 'Reviewed');frm.refresh_field('msds_status'); frm.save() }
                if (frm.doc.msds_status == 'Pending' || frm.doc.msds_status == 'Reject') {frappe.msgprint('MSDS状态未确认!')}
        },
        msds_unreview: function(frm) {
                if (frm.doc.msds_status == 'Unreviewed' || frm.doc.msds_status == 'Reviewed') {frm.set_value('msds_status', 'Reject');frm.refresh_field('msds_status'); frm.save() }
                if (frm.doc.msds_status == 'Pending') {frappe.msgprint('MSDS状态未确认!')}
        },
        // RoHS
        rohs_supplier_check: function(frm) {
                if (frm.doc.rohs_status == 'Pending' || frm.doc.rohs_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                        () => { frm.set_value('rohs_status', 'Unreviewed');frm.refresh_field('rohs_status'); frm.save()  },'Continue',true
                        )
                }
                if (frm.doc.rohs_status == 'Reviewed') {frappe.msgprint('RoHS状态已审核!')}
        },
        rohs_review: function(frm) {
                if (frm.doc.rohs_status == 'Unreviewed') {frm.set_value('rohs_status', 'Reviewed');frm.refresh_field('rohs_status'); frm.save() }
                if (frm.doc.rohs_status == 'Pending' || frm.doc.rohs_status == 'Reject') {frappe.msgprint('RoHS状态未确认!')}
        },
        rohs_unreview: function(frm) {
                if (frm.doc.rohs_status == 'Unreviewed' || frm.doc.rohs_status == 'Reviewed') {frm.set_value('rohs_status', 'Reject');frm.refresh_field('rohs_status'); frm.save() }
                if (frm.doc.rohs_status == 'Pending') {frappe.msgprint('RoHS状态未确认!')}
        },
        // HF
        hf_supplier_check: function(frm) {
                if (frm.doc.hf_status == 'Pending' || frm.doc.hf_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                        () => { frm.set_value('hf_status', 'Unreviewed');frm.refresh_field('hf_status'); frm.save()  },'Continue',true
                        )
                }
                if (frm.doc.hf_status == 'Reviewed') {frappe.msgprint('HF状态已审核!')}
        },
        hf_review: function(frm) {
                if (frm.doc.hf_status == 'Unreviewed') {frm.set_value('hf_status', 'Reviewed');frm.refresh_field('hf_status'); frm.save() }
                if (frm.doc.hf_status == 'Pending' || frm.doc.hf_status == 'Reject') {frappe.msgprint('HF状态未确认!')}
        },
        hf_unreview: function(frm) {
                if (frm.doc.hf_status == 'Unreviewed' || frm.doc.hf_status == 'Reviewed') {frm.set_value('hf_status', 'Reject');frm.refresh_field('hf_status'); frm.save() }
                if (frm.doc.hf_status == 'Pending') {frappe.msgprint('HF状态未确认!')}
        },
        // REACH
        reach_supplier_check: function(frm) {
                if (frm.doc.reach_status == 'Pending' || frm.doc.reach_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                        () => { frm.set_value('reach_status', 'Unreviewed');frm.refresh_field('reach_status'); frm.save() },'Continue',true
                        )
                }
                if (frm.doc.reach_status == 'Reviewed') {frappe.msgprint('REACH状态已审核!')}
        },
        reach_review: function(frm) {
                if (frm.doc.reach_status == 'Unreviewed') {frm.set_value('reach_status', 'Reviewed');frm.refresh_field('reach_status'); frm.save() }
                if (frm.doc.reach_status == 'Pending' || frm.doc.reach_status == 'Reject') {frappe.msgprint('REACH状态未确认!')}
        },
        reach_unreview: function(frm) {
                if (frm.doc.reach_status == 'Unreviewed' || frm.doc.reach_status == 'Reviewed') {frm.set_value('reach_status', 'Reject');frm.refresh_field('reach_status'); frm.save() }
                if (frm.doc.reach_status == 'Pending') {frappe.msgprint('REACH状态未确认!')}
        },
        // Others
        others_supplier_check: function(frm) {
                if (frm.doc.others_status == 'Pending' || frm.doc.others_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                        () => { frm.set_value('others_status', 'Unreviewed');frm.refresh_field('others_status'); frm.save() },'Continue',true
                        )
                }
                if (frm.doc.others_status == 'Reviewed') {frappe.msgprint('其他资料状态已审核!')}
        },
        others_review: function(frm) {
                if (frm.doc.others_status == 'Unreviewed') {frm.set_value('others_status', 'Reviewed');frm.refresh_field('others_status'); frm.save() }
                if (frm.doc.others_status == 'Pending' || frm.doc.others_status == 'Reject') {frappe.msgprint('其他资料状态未确认!')}
        },
        others_unreview: function(frm) {
                if (frm.doc.others_status == 'Unreviewed' || frm.doc.others_status == 'Reviewed') {frm.set_value('others_status', 'Reject');frm.refresh_field('others_status'); frm.save() }
                if (frm.doc.others_status == 'Pending') {frappe.msgprint('其他资料状态未确认!')}
        },
        // Special Requirements
        special_supplier_check: function(frm) {
                if (frm.doc.special_status == 'Pending' || frm.doc.special_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                        () => { frm.set_value('special_status', 'Unreviewed');frm.refresh_field('special_status'); frm.save() },'Continue',true
                        )
                }
                if (frm.doc.special_status == 'Reviewed') {frappe.msgprint('特殊要求状态已审核!')}
        },
        special_review: function(frm) {
                if (frm.doc.special_status == 'Unreviewed') {frm.set_value('special_status', 'Reviewed');frm.refresh_field('special_status'); frm.save() }
                if (frm.doc.special_status == 'Pending' || frm.doc.special_status == 'Reject') {frappe.msgprint('特殊要求状态未确认!')}
        },
        special_unreview: function(frm) {
                if (frm.doc.special_status == 'Unreviewed' || frm.doc.special_status == 'Reviewed') {frm.set_value('special_status', 'Reject');frm.refresh_field('special_status'); frm.save() }
                if (frm.doc.special_status == 'Pending') {frappe.msgprint('特殊要求状态未确认!')}
        },
        // Self Test
        test_supplier_check: function(frm) {
                if (frm.doc.self_test_status == 'Pending' || frm.doc.self_test_status == 'Reject') {
                        frappe.warn('请确认是否完成当前所有资料?','确认后将不允许再修订,只有环保工程师驳回后才允许修订',
                        () => { frm.set_value('self_test_status', 'Unreviewed');frm.refresh_field('self_test_status'); frm.save() },'Continue',true
                        )
                }
                if (frm.doc.self_test_status == 'Reviewed') {frappe.msgprint('成品自测状态已审核!')}
        },
        test_review: function(frm) {
                if (frm.doc.self_test_status == 'Unreviewed') {frm.set_value('self_test_status', 'Reviewed');frm.refresh_field('self_test_status'); frm.save() }
                if (frm.doc.self_test_status == 'Pending' || frm.doc.self_test_status == 'Reject') {frappe.msgprint('成品自测状态未确认!')}
        },
        test_unreview: function(frm) {
                if (frm.doc.self_test_status == 'Unreviewed' || frm.doc.self_test_status == 'Reviewed') {frm.set_value('self_test_status', 'Reject');frm.refresh_field('self_test_status'); frm.save() }
                if (frm.doc.self_test_status == 'Pending') {frappe.msgprint('成品自测状态未确认!')}
        },
});
